package org.ding.api.controller.v1;

import com.alibaba.fastjson2.JSONObject;
import org.ding.infra.mail.MailClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**
 * @Author: ding
 * @Mail: dingpeng0831@hotmail.com
 * @CreateTime: 2023-03-25  11:15
 * @Description:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/v1/mail")
public class MailController {

    @Autowired
    private MailClient client;

    @Autowired
    private TemplateEngine engine;

    @Async
    @PostMapping("/sender")
    public ResponseEntity sendMessage(@RequestBody JSONObject json) {
        String name = json.getString("name");
        String message = json.getString("subject");
        String content = json.getString("content");
        Context context = new Context();
        context.setVariable("content", content);
        String process = engine.process("/templates/mail.html", context);
        System.out.println(process);
        client.sendMail(name, message, process);
        return ResponseEntity.ok().build();
    }
}
